﻿using JXC.Core.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JXC.Core.Repository;
using JXC.EFCore;
using Microsoft.EntityFrameworkCore;
using AutoMapper;
using JXC.Application.Dtos;
using JXC.Utility;
using JXC.Application.Services.RoleInfoServices;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 角色信息
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RoleInfoController : BaseController
    {
        private readonly AppDbContext _context;
        private readonly IRepository<RoleInfos, Guid> _repository;
        private readonly IMapper _mapper;
        private readonly IRoleInfoService _roleInfoService;

        public RoleInfoController(AppDbContext context,IRepository<RoleInfos,Guid>repository,IMapper mapper,IRoleInfoService roleInfoService)
        {
            _context = context;
            _repository = repository;
            _mapper = mapper;
            _roleInfoService = roleInfoService;
        }
       

        // GET api/<RoleInfoController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }
        /// <summary>
        /// 获取所有的角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<RoleInfos>>> GetAllRoles()
        {
            return await _context.RoleInfos.ToListAsync();
        }
      
        // PUT api/<RoleInfoController>/5
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(Guid id, [FromBody] RoleInfos item)
        {
            if (item == null)
            {
                return BadRequest();
            }
            else
            {
                var tode = _context.RoleInfos.SingleOrDefault(t => t.Id== id);
                if (tode == null)
                {
                    return NotFound();
                }
                else
                {
                    //tode.IsComplete = item.IsComplete;
                    tode.RoleName = item.RoleName;
                    //tode.Remark = item.Remark;
                    _context.RoleInfos.Update(tode);
                    await _context.SaveChangesAsync();
                    return Ok(tode);
                }
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id">根据id</param>
        /// <returns></returns>
        // DELETE api/<RoleInfoController>/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            var tode = _context.RoleInfos.SingleOrDefault(t => t.Id == id);
            if (tode == null)
            {
                return NotFound();
            }
            else
            {
                _context.RoleInfos.Remove(tode);
                await _context.SaveChangesAsync();
                return Ok();
            }
        }
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="roleinfos"></param>
        /// <returns></returns>
        [Route("GetRoleInfoList")]
        [HttpGet]
        public async Task<ActionResult<IEnumerable<RoleInfos>>> GetRoleInfoList()
        {


            return await _context.RoleInfos.ToListAsync();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="roleInfo"></param>
        /// <returns></returns>
        [Route("AddRoleInfo")]
        [HttpPost]
        public async Task<IActionResult> AddRoleInfo( RoleInfoDtos roleInfo)
        {
            RoleInfos role = await _repository.InsertAsync(_mapper.Map<RoleInfos>(roleInfo));
            if (role == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
        }
        /// <summary>
        /// 根据id查找数据回显到前台
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("EditRoleInfoid")]
        [HttpPost]
        public async Task<IActionResult> EditRoleInfoid([FromBody] RoleInfoDto id)
        {
            RoleInfos user = await _repository.FirstOrDefaultAsync(s => s.Id==id.Id);
            if (user == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(DataResult(user));
        }
        [Route("EditRoleInfo")]
        [HttpPost]
        public async Task<IActionResult> EditRoleInfo(RoleInfoDto roleInfoDto)
        {
            RoleInfos user = await _repository.UpdateAsync(_mapper.Map<RoleInfos>(roleInfoDto));
            if (user == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
        }
        [Route("DelRoleInfo")]
        [HttpDelete]
        public async Task<IActionResult> DelRoleInfo(Guid id)
        {
            await _repository.DeleteAsync(s => s.Id == id);
            return Json(AjaxResult(true));
        }
    }
}
